import 'package:flutter/material.dart';

import '../../class/gain/all_mute.dart';
import '../../commont/base_controller.dart';
import '../../model/model.dart';
import '../../model/model_default.dart';
import '../../notify/globalState.dart';
import '../cross/cross_group.dart';
import 'gain_db.dart';

class GainController extends BaseController {
  GainController({super.key});
  @override
  State<GainController> createState() => kstate = _GainControllerState();
}

class _GainControllerState extends BaseControllerState<GainController> {
  late final List<List<GroupModel>> _list;
  @override
  void initState() {
    super.initState();
    _list = [group_list];
  }

  @override
  void copy(ChannelModel model, ChannelModel from) {
    model.mute = from.mute;
    model.phase = from.phase;
    model.db = from.db;
  }

  @override
  Widget buildContent(BuildContext context) {
    return Column(children: [
      GaidDbView(
          ch_list: globalModel.ch_list,
          onChange: (ch, tag) {
            setState(() {
              unitChannel(ch);
            });
            if (tag == 'DB') {
              _send_data('DB', ch);
            } else if (tag == 'PHASE') {
              _send_data('PHASE', ch);
            }
          },
          onChangeForSend: (ch, tag) {
            _send_data('DB', ch);
          }),
      AllMute(
        onChange: () {
          _send_data('MUTE_ALL', current_ch);
        },
      ),
      GroupView(
        list: _list,
        onClick: onclickGroup,
        onLongPress: onLongPressGroup,
        enable: true,
      ),
    ]);
  }
}

///发送数据
extension GainControlleExtension on _GainControllerState {
  void _send_data(String tag, ChannelModel ch) {
    if ((tag != 'MUTE_ALL' && tag != 'PHASE' && tag != 'DB') || ch.name == '')
      return;
    if (tag == 'MUTE_ALL') {
      globalState.state = NotifyData(key: tag, body: ch);
      return;
    }
    globalState.state = NotifyData(key: tag, body: ch);
    for (var group in select_group_list) {
      if (group.contain(ch)) {
        ChannelModel other_ch =
            group.ch1!.num == ch.num ? group.ch2! : group.ch1!;
        globalState.state = NotifyData(key: tag, body: other_ch);
      }
    }
  }
}
